package com.service.thirddatacenter.hpool.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.thirddatacenter.hpool.mapper.MiningRecordMapper;
import com.service.thirddatacenter.hpool.model.entity.AccountThirdEntity;
import com.service.thirddatacenter.hpool.model.entity.MiningRecordEntity;
import com.service.thirddatacenter.hpool.service.AccountThirdService;
import com.service.thirddatacenter.hpool.service.BasePlatformService;
import com.service.thirddatacenter.hpool.service.MiningRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 收益记录
 *
 * @date 2021-06-08 15:41:03
 */
@Slf4j
@Service("miningRecordService")
public class MiningRecordServiceImpl extends ServiceImpl<MiningRecordMapper, MiningRecordEntity> implements MiningRecordService {

    @Autowired
    private AccountThirdService accountThirdService;

    @Autowired
    private BasePlatformService hpoolService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncMiningRecord() {
        List<AccountThirdEntity> accountThirdList = accountThirdService.listAllAccount();
        if (Objects.isNull(accountThirdList)) {
            log.info("暂无第三方平台账号，获取挖矿收益完成");
            return;
        }

        accountThirdList.parallelStream().forEach(account->{
            switch (account.getPlatform()){
                case "hpool":
                    hpoolService.syncMiningRecord(account);
                    return;
                default:
                    log.info("{}未实现", account);
                    return;
            }
        });
    }

    @Override
    public MiningRecordEntity getLastMiningRecord(Integer accountId, String type) {
        return getOne(Wrappers.lambdaQuery(MiningRecordEntity.class)
                .eq(MiningRecordEntity::getAccountId, accountId)
                .eq(MiningRecordEntity::getMiningType, type)
                .orderByDesc(MiningRecordEntity::getRecordTime).last("limit 1"));
    }

    /**
     * @param accountId
     * @param type
     * @Description: 统计商户某天的累计收益
     * @Author: ty
     * @Date: 2021/6/22 15:34
     * @param: [accountIds, type, someday]
     * @return: java.math.BigDecimal
     */
    @Override
    public BigDecimal getLastIncomeByAccountId(Integer accountId, String type) {
        return baseMapper.getLastIncomeByAccountId(accountId, type);
    }
}
